﻿using System;
using System.Linq;
using System.Xml.Linq;
using DACU.Player.Enums;
using DACU.VkontakteApi.Enums;
using Un4seen.Bass;

namespace DACU.Tools
{
	static class XmlUtils
	{
		public static string GetNodeString(XElement node)
		{
			return node != null ? Utils.HtmlDecode(node.Value) : null;
		}

		public static bool? NodeValueToBool(XElement node)
		{
			if (node == null) return null;
			return node.Value == "1" ? (bool?)true : (node.Value == "0" ? (bool?)false : null);
		}

		public static VKSex NodeValueToVKSex(XElement node)
		{
			if (node == null) return VKSex.None;
			return node.Value == VKSex.Male.ToString("d")
					? VKSex.Male
					: (node.Value == VKSex.Female.ToString("d") ? VKSex.Female : VKSex.None);
		}

		public static int[] NodeValueToArray(XElement node)
		{
			if (node == null || node.IsEmpty) return null;
			var ints = node.Elements("lid");
			return ints == null?
				null
				:
				ints.Where(elem => !String.IsNullOrWhiteSpace(elem.Value)).Select(elem => Convert.ToInt32(elem.Value)).ToArray();
		}

		public static string GetStringFromAttribute(this XElement element,string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			return att == null || att.Value == null ? "" : att.Value;
		}

		public static int GetIntFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return -1;
			int value;
			if (int.TryParse(att.Value, out value))
				return value;
			return -1;
		}

		public static double GetDoubleFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return double.NaN;
			double value;
			if (double.TryParse(att.Value, out value))
				return value;
			return double.NaN;
		}

		public static float GetFloatFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return float.NaN;
			float value;
			if (float.TryParse(att.Value, out value))
				return value;
			return float.NaN;
		}

		public static bool GetBoolFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return false;
			bool value;
			if (bool.TryParse(att.Value, out value))
				return value;
			return false;
		}

		public static BASSActive GetBASSActiveFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return default(BASSActive);
			BASSActive value;
			if (Enum.TryParse(att.Value, out value))
				return value;
			return default(BASSActive);
		}

		public static TrackType GetTrackTypeFromAttribute(this XElement element, string attributeName)
		{
			XAttribute att = element.Attribute(attributeName);
			if (att == null) return default(TrackType);
			TrackType value;
			if (Enum.TryParse(att.Value, out value))
				return value;
			return default(TrackType);
		}
	}
}